package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.GoodsExt;
import com.qianniu.napi.admin.service.IGoodsExtService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 服务商品扩展表
 * </p>
 *
 * @author chenxuebin
 * @since 2018-08-01
 */
@Controller
public class GoodsExtController {

    private static final Logger logger = LoggerFactory.getLogger(GoodsExtController.class);

    @Autowired
    private IGoodsExtService goodsextService;

    @RequiresPermissions("admin:user:view")
    @RequestMapping("admin/goodsext/index")
    public String index() {
        return "admin/goodsext";
    }

    @RequiresPermissions("admin:user:view")
    @RequestMapping("/admin/goodsext/list")
    @ResponseBody
    public Page<GoodsExt> alist() {
        Page<GoodsExt> page = goodsextService.selectPage(new Page<GoodsExt>(0, 10));
        return page;
    }

    @RequestMapping("/admin/goodsext/add")
    public String add(@Valid GoodsExt bean, BindingResult bindingResult) {
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            return null;
        }
        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setUid(ObjectUtil.getUid());
        bean.insert();
        return "admin/goodsext";
    }

    @RequestMapping("/admin/goodsext/getGoodsExtById")
    @ResponseBody
    public GoodsExt getGoodsExtById(long id) {
        GoodsExt goodsext = goodsextService.selectById(id);
        return goodsext;
    }

    @RequestMapping("/admin/goodsext/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = goodsextService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/goodsext/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        GoodsExt bean = new GoodsExt();
        bean.setId(id);
        bean.setState(state);
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        goodsextService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/goodsext/modify")
    public String update(GoodsExt bean) {
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        boolean isSuccc = goodsextService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }
}
